<template>
  <el-row class="login">
    <el-col :span="12" class="left_pic">
    </el-col>
    <el-col :span="6" :offset="3" class="form">
      <div class="container">
        <!-- 注册表单 -->
        <!-- <el-form v-if="isRegister" :rules="rules" size="large" :module="userData"> -->
        <el-form v-if="isRegister" :rules="rules" size="large" :model="userData">
          <!-- 我真的会谢： -->
          <el-form-item>
            <h1>注册</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="userData.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefixIcon="Lock" placeholder="请输入密码" v-model="userData.password"></el-input>
          </el-form-item>
          <el-form-item prop="rePassword">
            <el-input :prefixIcon="Lock" placeholder="请再次输入密码" v-model="userData.rePassword"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="register">注册</el-button>
          </el-form-item>
          <el-form-item>
            <el-link type="info" :underline="false" @click="isRegister = false, clearUserData()">⬅ 返回</el-link>
          </el-form-item>
        </el-form>
        <!-- 登录表单 -->
        <el-form v-else :rules="rules" size="large" :model="userData">
          <el-form-item>
            <h1>登录</h1>
          </el-form-item>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="userData.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefixIcon="Lock" placeholder="请输入密码" v-model="userData.password"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="flex">
              <el-checkbox>记住我</el-checkbox>
              <el-link type="primary" :underline="false" style="display: block;float: right;">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
          </el-form-item>
          <el-form-item>
            <!-- TODO:此处clearUserData一定要带上括号的原因【待解决2】 -->
            <el-link type="info" :underline="false" @click="isRegister = true, clearUserData()">注册 ➡</el-link>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { userLoginApi, userRegisterApi } from '@/api/user';
import { SET_TOKEN } from '@/utils/token';
import { Lock, User } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';

// 路由器对象
const router = useRouter()

const userData = reactive({
  username: '',
  password: '',
  rePassword: ''
})

// 注册还是登录界面的判断标识
const isRegister = ref(false)

// 校验密码
const checkPW = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('密码不能为空'))
  }
  else if (value !== userData.password) {
    callback(new Error('两次输入的密码不一致'))
  }
  callback()
}

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'change' },
    { min: 5, max: 16, message: '长度为5~16位的非空字符', trigger: 'change' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'change' },
    { min: 5, max: 12, message: '长度为5~12位的非空字符', trigger: 'change' }
  ],
  rePassword: [
    { validator: checkPW, trigger: 'change' }
  ]
}

// TODO:注册成功清除表单数据之后，表单校验会再次触发问题【待解决1】【直接令其跳转到登录界面】
// 注册
const register = async () => {
  let res = await userRegisterApi(userData)
  // if (res.code == 0) {
  //   ElMessage.success('注册成功！')
  // } else {
  //   ElMessage.error('注册失败！')
  // }
  ElMessage.success(res.message)
  isRegister.value = false//跳到登录界面
  clearUserData()
}

// 清除表单数据
const clearUserData = () => {
  userData.username = ''
  userData.password = ''
  userData.rePassword = ''
}

// 登录
const login = async () => {
  let res = await userLoginApi(userData);
  // if(res.code == 0){
  //   SET_TOKEN(res.data)
  //   ElMessage.success('登录成功')
  // }else{
  //   ElMessage.error('登录失败')
  // }
  SET_TOKEN(res.data)
  ElMessage.success(res.message)
  // 编程式导航
  router.replace('/')
  clearUserData()
}
</script>

<style scoped lang="scss">
.login {
  height: 100vh;
  // background-color: #008c8c;

  .left_pic {
    background: url(../assets/logo2.png) no-repeat 60% center / 240px auto,
      url(../assets/login_bg.jpg) no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }

  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;

    .container {
      box-shadow: 2px 7px 20px 1px rgba(0, 0, 0, 0.2);
      padding: 20px;

      h1 {
        width: 100%;
        color: #008c8c;
        text-align: center;
      }

      .button {
        width: 100%;
      }

      .flex {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }

  }
}
</style>